//
//  HeartRateProgressCell.m
//  iiecg
//
//  Created by Shanlin on 2017/3/23.
//  Copyright © 2017年 Boundary Technology. All rights reserved.
//

#import "HeartRateProgressCell.h"

@interface HeartRateProgressCell ()
@property (weak, nonatomic) IBOutlet UIView *rootView;
@property (nonatomic,strong) NSMutableArray<UIView*> *views;//
@property (nonatomic,strong) NSArray<NSString*> *values;//
@property (nonatomic,strong) NSArray<NSString*> *labelValue;//
@property (nonatomic,strong) HeartRateProgressBean *bean;//
@property (nonatomic,strong) NSArray<UIColor*> *colors;//
@end

@implementation HeartRateProgressCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

-(NSMutableArray<UIView *> *)views
{
    if (!_views) {
        _views = [[NSMutableArray alloc] init];
    }
    return _views;
}

-(void)setBean:(HeartRateProgressBean *)bean
{
    __weak typeof(self)weakSelf = self;
    _bean = bean;
    [IKPublicUtil runOnMainThread:^{
        [weakSelf createProgress];
        [weakSelf updateProgress:_bean.heartRate];
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


+(instancetype)createWith:(UITableView*)tableView bean:(HeartRateProgressBean*)bean
{
    static NSString *ID = @"HeartRateProgressCell";
    HeartRateProgressCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:ID owner:nil options:nil] lastObject];
    } else {
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    cell.bean = bean;
    return cell;

}

-(void)createProgress{
    __weak typeof(self)weakSelf = self;
    for (UIView *v in weakSelf.views) {
        [v removeFromSuperview];
    }
    [weakSelf.views removeAllObjects];
    CGFloat width =  KScreenW - 16 * 2;
    weakSelf.values = @[
                        @"",
                        @"55",
                        @"60",
                        @"100",
                        @"",
                        ];
    weakSelf.labelValue = @[
                            INTERNATIONAL(@"string_low"),
                            INTERNATIONAL(@"string_dazhizhengchang"),
                            INTERNATIONAL(@"string_nomal_zhengcang"),
                            INTERNATIONAL(@"string_quick"),
                            ];
    weakSelf.colors = @[
                        ECG_COLOR_ORANGE,
                        ECG_COLOR_GREEN,
                        ECG_COLOR_GREEN,
                        ECG_COLOR_ORANGE,
                        ];
    CGFloat width_1 = width / (weakSelf.labelValue.count*1.0f); // 数字宽度
    CGFloat x = 16;//x点
    CGFloat y = 0;//y点
    NSTextAlignment textAlignment = NSTextAlignmentCenter;
    for (int i = 0; i < weakSelf.values.count; i++) { // 数字
        CGFloat w = width_1;
        if (i == 0) {
            w = width_1 / 2.0f;
            textAlignment = NSTextAlignmentLeft;
        }else if (i == weakSelf.values.count - 1) {
            w = width_1 / 2.0f;
            textAlignment = NSTextAlignmentRight;
        }else{
            textAlignment = NSTextAlignmentCenter;
        }
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, 21)]; 
        label.textAlignment = textAlignment;
        label.font = [UIFont systemFontOfSize:14];
        label.text = [weakSelf.values objectAtIndex:i];
        label.textColor = COLOR_GREY_A8A8A8;
        [weakSelf.rootView addSubview:label];
        x = x + w;
    }
    x = 16;
    textAlignment = NSTextAlignmentCenter;
    y = y+ 21;
    CGFloat y2 = y + 8 + 8;
    CGFloat interval = 4;
    CGFloat width_2 = (width - (weakSelf.labelValue.count - 1) * interval) / weakSelf.labelValue.count;
    for (int i = 0; i < weakSelf.labelValue.count; i++) {
        UIView * progress = [[UIView alloc] initWithFrame:CGRectMake(x + i*interval, y, width_2, 8)];// 颜色条
        progress.backgroundColor = COLOR_GREY_E6E6E6;
        [weakSelf.views addObject:progress];
        [weakSelf.rootView addSubview:progress];
        
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(x + i*interval, y2, width_2, 21)];
        label.textAlignment = textAlignment;
        label.font = [UIFont systemFontOfSize:14];
//        label.adjustsFontSizeToFitWidth = YES;
        label.text = [weakSelf.labelValue objectAtIndex:i];
//        [label setNeedsDisplay];
        label.textColor = COLOR_GREY_A8A8A8;
        [weakSelf.rootView addSubview:label];
        x = x + width_2;
    }
    
}

-(void)updateProgress:(NSString*)progress
{
    __weak typeof(self)weakSelf = self;
    int p = -1;
    NSInteger hr = [progress integerValue];
//    if (hr < 50) {
//        return;
//    }
    if (hr < 55) {
        p = 0;
    }else if (hr < 60){
        p = 1;
    }else if (hr < 100){
        p = 2;
    }else{
        p = 3;
    }
    for (int i = 0; i<weakSelf.labelValue.count; i++) {
        UIView * value = [weakSelf.views objectAtIndex:i];
        if (p == i) {
            value.backgroundColor = [weakSelf.colors objectAtIndex:p];
        } else {
            value.backgroundColor = COLOR_GREY_E6E6E6;
        }
    }
}

@end
